package com.lanjie.test;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;

public class MainActivity extends Activity implements SensorEventListener{
	
	SensorManager sensorManager;
	Vibrator vibrator;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		sensorManager = (SensorManager)this.getSystemService(SENSOR_SERVICE);
		vibrator = (Vibrator)this.getSystemService(VIBRATOR_SERVICE);
	}

	@Override
	protected void onResume() {
		super.onResume();
		sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
	}


	@Override
	protected void onStop() {
		sensorManager.unregisterListener(this);
		super.onStop();
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		float values[] = event.values;
		StringBuilder sb = new StringBuilder();
		sb.append("X方向：");
		sb.append(values[0]);
		sb.append("\nY方向：");
		sb.append(values[1]);
		sb.append("\nZ方向：");
		sb.append(values[2]);
		if(Math.abs(values[0]) > 25 || Math.abs(values[1]) > 25 || Math.abs(values[2]) > 25){
			Log.d("--------", sb.toString());
			vibrator.vibrate(500);
		}
		//Toast.makeText(MainActivity.this, sb.toString(), Toast.LENGTH_LONG).show();
	}

}
